<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>文档</title>
  <link href="../css/semantic/semantic.min.css" rel="stylesheet">
  <link href="../css/markdown.css" rel="stylesheet">
    <link href="../css/wtxynavigation.css" rel="stylesheet">
    <script src="../js/jquery.min.js"></script>
    <script type="text/javascript" src="../js/include.js"></script>
  <style>
    body{padding-top:0;background:#f7f7f7}
    body>.ui.container{width:100%;padding:0}
    #project-title{background:#6e6e6e;width:100%;line-height:3.572rem;color:#fff;font-size:1.2rem}
    #project-title .title-wrap{font-weight:700;padding:0 12px;width:100%;max-width:1366px!important;min-width:900px;margin:0 auto}
    #project-title .left,#project-title right{display:inline-block}
    #project-title .right{float:right}
    #project-title .right .wiki-actions .menu{left:auto;right:0;margin-top:.4em!important}
    #wiki-preview-container{width:100%;max-width:1366px!important;min-width:900px;padding:0;border:1px solid #e3e9ed;border-top:0}
    #wiki-preview>.ui.segment{margin-top:0;padding:1rem;padding-left:0;border:0}
    .hover-places{display:inline-block}
    #page-detail{width:60%;margin:0 auto}
    #page-detail .title{font-weight:700;font-size:24px;padding:12px 0;border-bottom:1px solid #ccc}
    #page-detail .content{margin-top:15px}
    .content p{word-wrap:break-word}
    #wiki-parent,#wiki-title,#wikis{display:none}
    .ui.grid{padding:0!important}
    #ex-all{cursor:pointer;color:#537c8d}
  </style>
</head>
<body>
  <div class="ui container">
    <div id="project-title">
      <div class="title-wrap">
        <div class="left">
          <i class="icon eye"></i>
          文档预览:
          mianmSDK
        </div>
        <div class="right">
          Export by Gitee
        </div>
      </div>
    </div>

    <div class="ui container" id="wiki-preview-container">
      <div id="wiki-preview">
        <div class="ui segment">
          <div id="page-detail" class="markdown-body">
            <div class='title'>29.runtime动态添加方法</div><div class='content'><h1>
<a id="runtime动态添加方法" class="anchor" href="#runtime%E5%8A%A8%E6%80%81%E6%B7%BB%E5%8A%A0%E6%96%B9%E6%B3%95"></a>runtime动态添加方法</h1>
<p><strong>关键词</strong>: <code>runtime</code>,<code>performSelector</code>,<code>class_addMethod</code>,<code>method_t</code></p>
<h2>
<a id="描述" class="anchor" href="#%E6%8F%8F%E8%BF%B0"></a>描述</h2>
<h3>
<a id="1-performselector" class="anchor" href="#1-performselector"></a>1. performSelector</h3>
<p>一个类它编译时没有这个方法, 然后动态运行的时候有了这个方法, 我们就用<code>[self  performSelector:@selector(xxxxx)];</code>这样去调用..</p>
<h1>
<a id="2-class_addmethod" class="anchor" href="#2-class_addmethod"></a>2. class_addMethod</h1>
<ul>
<li>其中后三位就是<code>method_t</code>
</li>
</ul>
<pre><code>
class_addMethod(Class cls, SEL name, IMP imp, const char * types);

</code></pre>
<p><img src="images/runtimemethodAdd01.png" alt="runtimemethodAdd01"></p>
<p><img src="../20210513/images/runtimeAll.png" alt="runtimeAll"></p>
<h1>
<a id="示例代码" class="anchor" href="#%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81"></a>示例代码:</h1>
<pre><code>
#import &lt;objc/runtime.h&gt;

@interface WTUserCenter ()


@end

@implementation WTUserCenter



void cctv(void)
{
    
    NSLog(@"🙂%s", __func__);
}
/**
 实例方法调用
 sel : 方法选择器

 返回YES: 消息已处理, 结束消息转发流程
 返回NO: 系统给我们第二次机会⏬ forwardingTargetForSelector
 */
+ (BOOL)resolveInstanceMethod:(SEL)sel{
    

    if (sel == @selector(test)) {
        
        NSLog(@"🙂%s", __func__);
        
        
        
        class_addMethod(self, @selector(test), cctv, "v@:");
        return NO;
    }else{
        
        return [super resolveInstanceMethod:sel];
    }
}

@end

</code></pre></div>
          </div>
        </div>
      </div>
      </div>
  </div>
</body>
</html>